

package yf.mall.service.impl.app;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.response.RRException;
import com.yf.service.impl.BaseServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.dao.AppMaterialDao;
import yf.mall.dao.AppProductMaterialDao;
import yf.mall.dao.KolProductDao;
import yf.mall.entity.AppMaterialEntity;
import yf.mall.entity.AppMaterialEntityExample;
import yf.mall.entity.AppProductMaterialEntity;
import yf.mall.entity.KolProductEntity;
import yf.mall.mapper.AppMaterialExtMapper;
import yf.mall.service.app.AppMaterialService;
import yf.mall.vo.AppMaterialVo;
import yf.mall.vo.down.AppMaterialDown;
import yf.mall.vo.up.AppMaterialUp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 账户记录Service实现类
 *
 *
 *
 * @date Tue Jul 18 14:30:26 CST 2023
 */
@Service
public class AppMaterialServiceImpl extends BaseServiceImpl<AppMaterialEntity> implements AppMaterialService {

    @Resource
    private AppMaterialDao dao;

    @Resource
    private AppMaterialExtMapper appMaterialExtMapper;


    @Autowired
    public void setDao(AppMaterialDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }


    @Override
    public List<AppMaterialVo> findVo(AppMaterialVo vo) {
        return dao.findVo(vo);
    }

    @Override
    public AppMaterialVo getVo(Long id) {
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(AppMaterialVo vo) {
        dao.save(vo.getEntity());
        if(vo.getProductId()!=null){
            KolProductEntity productEntity = kolProductDao.get(vo.getProductId());
            if(productEntity==null){
                throw new RRException("产品ID不对!");
            }
            AppProductMaterialEntity appProductMaterialEntity = new AppProductMaterialEntity(vo.getEntity().getId(), vo.getProductId(), new Date());
            appProductMaterialDao.save(appProductMaterialEntity);
        }
        return 1;
    }

    @Resource
    private KolProductDao kolProductDao;

    @Resource
    private AppProductMaterialDao appProductMaterialDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(AppMaterialVo vo) {
        return dao.update(vo.getEntity());
    }


    @Override
    public void downLoad(HttpServletResponse response, List<AppMaterialVo> list) {
        try {
            List<AppMaterialDown> datas = list.stream().map(item -> new AppMaterialDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), AppMaterialDown.class).sheet("模板").doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, AppMaterialUp.class, new UploadDataListener()).sheet().doRead();
    }

    @Override
    public List<AppMaterialEntity> listByIds(List<Long> materialIds) {
        return listByMaterialIds(materialIds, null);
    }

    @Override
    public PageInfo<AppMaterialEntity> page(AppMaterialEntityExample example, Page<Object> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<AppMaterialEntity> entityList = appMaterialExtMapper.selectByExample(example);
        return new PageInfo(entityList);
    }

    @Override
    public List<AppMaterialEntity> listByMaterialIds(List<Long> materialIds, Integer fileType) {
        if (CollectionUtils.isEmpty(materialIds)) {
            return Collections.emptyList();
        }
        AppMaterialEntityExample example = new AppMaterialEntityExample();
        example.createCriteria().andIdIn(materialIds).andFileTypeEqualTo(fileType);
        return appMaterialExtMapper.selectByExample(example);
    }

    class UploadDataListener extends AnalysisEventListener<AppMaterialUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(AppMaterialUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }


    /**内部方法**/


    /**外部方法实现**/

    /**
     * id title
     * @return
     */
    @Override
    public List<AppMaterialEntity> findAll() {
        return dao.findAll();
    }
}
